package coreservlets.filters;

import java.io.IOException;

public class ReplaceSiteNameFilter extends ModificationFilter {
	private boolean isCaseInsensitive = false;

	public String getTarget() {
		return getInitParameter("target");
	}

	public String getReplacement() {
		return getInitParameter("replacement");
	}

	private String getInitParameter(String param) {
		String value = config.getInitParameter(param);
		if (value == null || value.isEmpty()) {
			value = null;
		}
		return value;
	}

	@Override
	public String doModification(String origResponse) throws IOException {
		if (getTarget() == null || getReplacement() == null) {
			return origResponse;
		}else {
			String target = getTarget();
			if (isCaseInsensitive) {
				target = "(?i)" + target;
			}
			String replacement = getReplacement();
			return origResponse.replaceAll(target, replacement);
		}
	}

	public void setCaseInsensitive(boolean isCaseInsensitive) {
		this.isCaseInsensitive = isCaseInsensitive;
	}

	public boolean isCaseInsensitive() {
		return isCaseInsensitive;
	}

}
